<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:fn="http://java.sun.com/jsp/jstl/functions"
                xmlns:p="http://primefaces.org/ui">
    <h:panelGrid columns="7" >
        <h:outputLabel value="Segmentación Ult. Año" />
        <p:selectOneMenu id="cmbSegmentacion" required="true"
                              requiredMessage="*"
                              value="#{jsfBean.reporteContext.tipoSegmentacionCxA}"
                              converter="#{managerTipoSegmentacionConverter}"
                >
            <f:selectItems
                    value="#{reporteCuentaAlmacenJsfBean.allTipoSegmentacion}"
                    var="reporteItem"
                    itemLabel="#{reporteItem.nombre}" itemValue="#{reporteItem}"/>

        </p:selectOneMenu>
            <p:spacer style="width: 50px" />
        <h:outputLabel value="Rango" />
        <p:selectOneMenu id="cmbRango" required="true" style="width: 100px"
                              requiredMessage="*"
                              value="#{jsfBean.reporteContext.rangoSelect}"
                >
            <f:selectItems
                    value="#{reporteCuentaAlmacenJsfBean.allRangoList}"
                    var="rangoItem"
                    itemLabel="#{rangoItem.descripcion}" itemValue="#{rangoItem.rangoId}"/>
        </p:selectOneMenu>
        <p:spacer width="5%" />
        <p:commandButton value="Crear Rango" icon="ui-icon-refresh"
                         process="@this"
                         actionListener="#{jsfBean.showDialog}"
                         ajax="true" update=":seleccionDialog :messages">
            <f:attribute name="dialogName" value="wvSeleccionDialog"/>
        </p:commandButton>

        <p:commandButton value="Buscar" icon="ui-icon-refresh"
                         process="@this,cmbSegmentacion,cmbRango"
                         actionListener="#{jsfBean.onSelectFiltro}"
                         ajax="true" update="#{ui_dtLista} :messages"
                />
    </h:panelGrid>
    <br/>
    <p:toolbar>
        <p:toolbarGroup align="left">
            <p:commandButton ajax="false" process="@this" value="Exportar Detalle - Total General"
                           actionListener="#{jsfBean.printExcelReportGlobalDetailAction}">
                <f:attribute name="templateFile" value="#{jasperReporte}"/>
                <f:attribute name="templateType" value="xls"/>
                <f:attribute name="reportType" value="#{tipoReporte}"/>
            </p:commandButton>
        </p:toolbarGroup>
        <p:toolbarGroup align="right">
            <p:commandLink ajax="false" process="@this"
                           actionListener="#{jsfBean.printExcelReportGlobalAction}">
                <p:graphicImage value="../resources/images/newExcel.png" width="25px" height="25px"/>
                <f:attribute name="templateFile" value="#{jasperReporte}"/>
                <f:attribute name="templateType" value="xls"/>
                <f:attribute name="reportType" value="#{tipoReporte}"/>
            </p:commandLink>
        </p:toolbarGroup>
    </p:toolbar>
    <p:dataTable id="#{ui_dtLista}" value="#{data_reporte}"
                 var="item" rowKey="#{item.id}"
                 rows="10" paginator="true"
                 paginatorPosition="bottom" emptyMessage="No existen registros"
                 paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                 rowsPerPageTemplate="5,10,25,50">
        <p:columnGroup type="header">
            <p:row  >
                <p:column colspan="3" headerText="Totales: "  style="text-align:right" />
                <p:column  styleClass="columnRightTopClass">
                    <f:facet name="header" >
                        <h:outputText value="#{totalBeanReporte.totalCantidad1}">
                            <f:convertNumber pattern="###,###,##0" locale="en-US"/>
                        </h:outputText>
                    </f:facet>
                </p:column>
                <p:column  styleClass="columnRightTopClass">
                    <f:facet name="header" >
                        <h:outputText value="#{totalBeanReporte.totalAnno1}">
                            <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                        </h:outputText>
                    </f:facet>
                </p:column>
                <p:column  styleClass="columnRightTopClass">
                    <f:facet name="header" >
                        <h:outputText value="#{totalBeanReporte.totalCantidad2}">
                            <f:convertNumber pattern="###,###,##0" locale="en-US"/>
                        </h:outputText>
                    </f:facet>
                </p:column>
                <p:column  styleClass="columnRightTopClass" >
                    <f:facet name="header">
                        <h:outputText value="#{totalBeanReporte.totalAnno2}">
                            <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                        </h:outputText>
                    </f:facet>

                </p:column>
                <p:column  styleClass="columnRightTopClass">
                    <f:facet name="header" >
                        <h:outputText value="#{totalBeanReporte.totalCantidad3}">
                            <f:convertNumber pattern="###,###,##0" locale="en-US"/>
                        </h:outputText>
                    </f:facet>
                </p:column>
                <p:column  styleClass="columnRightTopClass" >
                    <f:facet name="header">
                        <h:outputText value="#{totalBeanReporte.totalAnno3}">
                            <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                        </h:outputText>
                    </f:facet>

                </p:column>
                <c:if test="#{fn:length(jsfBean.columns)/2 eq 1}">
                    <p:column  styleClass="columnRightTopClass">
                        <f:facet name="header" >
                            <h:outputText value="#{totalBeanReporte.totalCantidad4}">
                                <f:convertNumber pattern="###,###,##0" locale="en-US"/>
                            </h:outputText>
                        </f:facet>
                    </p:column>
                    <p:column  styleClass="columnRightTopClass" >
                        <f:facet name="header">
                            <h:outputText value="#{totalBeanReporte.totalAnno4}">
                                <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                            </h:outputText>
                        </f:facet>

                    </p:column>
                </c:if>
                <c:if test="#{fn:length(jsfBean.columns)/2 ne 1}">
                    <c:forEach items="#{jsfBean.columns}" var="columnx"  >
                        <p:column  styleClass="columnRightTopClass"  >
                            <f:facet name="header">
                                <h:outputText value="#{totalBeanReporte[columnx.property]}">
                                    <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                                </h:outputText>
                            </f:facet>
                        </p:column>
                    </c:forEach>
                </c:if>
                <p:column   styleClass="columnRightTopClass">
                    <f:facet name="header">
                        <h:outputText value="#{totalBeanReporte.totalCantidad}">
                            <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                        </h:outputText>
                    </f:facet>
                </p:column>
                <p:column   styleClass="columnRightTopClass">
                    <f:facet name="header">
                        <h:outputText value="#{totalBeanReporte.totalGeneral}">
                            <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                        </h:outputText>
                    </f:facet>
                </p:column>
            </p:row>
            <p:row >
                <p:column colspan="3" headerText="" />
                <p:column colspan="2" headerText="Hasta #{jsfBean.reporteContext.checkPoint.anno1}" />
                <p:column colspan="2" headerText="#{jsfBean.reporteContext.checkPoint.anno2}" />
                <p:column colspan="2" headerText="#{jsfBean.reporteContext.checkPoint.anno3}" />

                <c:if test="#{fn:length(jsfBean.columns)/2 eq 1}">
                    <p:column colspan="2" headerText="#{jsfBean.reporteContext.checkPoint.anno4}" />
                </c:if>
                <c:if test="#{fn:length(jsfBean.columns)/2 ne 1}">
                    <c:forEach begin="1" end="#{fn:length(jsfBean.columns)/2}" varStatus="item" >
                        <p:column colspan="2" headerText="#{jsfBean.reporteContext.checkPoint.anno4}-#{(item.index)}" />
                    </c:forEach>
                </c:if>

                <p:column colspan="2" headerText="Total General" />
            </p:row>

            <p:row >
                <p:column headerText="Grupo Empresarial" />
                <p:column headerText="Sociedad" />
                <p:column headerText="Rango" sortBy="#{item.rangoLabel}" />
                <p:column headerText="cantidad" sortBy="#{item.cantidadAnno1}"/>
                <p:column headerText="total" sortBy="#{item.anno1}"/>
                <p:column headerText="cantidad" sortBy="#{item.cantidadAnno2}"/>
                <p:column headerText="total" sortBy="#{item.anno2}"/>
                <p:column headerText="cantidad" sortBy="#{item.cantidadAnno3}" />
                <p:column headerText="total" sortBy="#{item.anno3}" />
                <c:forEach begin="1" end="#{fn:length(jsfBean.columns)/2}">
                    <p:column headerText="cantidad" />
                    <p:column headerText="total" />
                </c:forEach>

                <p:column colspan="2" headerText="total" />
            </p:row>

        </p:columnGroup>
        <p:column styleClass="columnLeftTopClass" style="min-width:100px">
            <h:outputText value="#{item.grupoEmpresarialNombre}"/>
        </p:column>
        <p:column styleClass="columnLeftTopClass" style="width:60px">
            <h:outputText value="#{item.sociedadId}"/>
        </p:column>
        <p:column styleClass="columnLeftTopClass" style="min-width:100px" >
            <h:outputText  value="#{item.rangoLabel}"/>
        </p:column>
        <p:column styleClass="columnRightTopClass" style="min-width:30px">
            <h:outputText styleClass="columnRightTopClass"  value="#{item.cantidadAnno1}">
                <f:convertNumber pattern="###,###,##0" locale="en-US"/>
            </h:outputText>
        </p:column>
        <p:column styleClass="columnRightTopClass" style="min-width:30px">
            <h:commandLink action="#{jsfBean.onDetalleItemGlobal}" ajax="false"
                            update=":detalleAlmacenGlobalItemForm" target="_blank">
                <h:outputText value="#{item.anno1}">
                    <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                </h:outputText>
                <f:setPropertyActionListener target="#{jsfBean.anno}" value="#{jsfBean.reporteContext.checkPoint.anno1}"/>
                <f:setPropertyActionListener target="#{jsfBean.sociedadId}" value="#{item.sociedadId}"/>
                <f:setPropertyActionListener target="#{jsfBean.checkPointId}" value="#{item.checkPointId}"/>
                <f:setPropertyActionListener target="#{jsfBean.rangoId}" value="#{jsfBean.reporteContext.rangoSelect}"/>
                <f:setPropertyActionListener target="#{jsfBean.rangoOrden}" value="#{item.rango}"/>
            </h:commandLink>
        </p:column>
        <p:column styleClass="columnRightTopClass" style="min-width:30px">
            <h:outputText styleClass="columnRightTopClass"  value="#{item.cantidadAnno2}">
                <f:convertNumber pattern="###,###,##0" locale="en-US"/>
            </h:outputText>
        </p:column>

        <p:column styleClass="columnRightTopClass" style="min-width:30px">

            <h:commandLink action="#{jsfBean.onDetalleItemGlobal}" ajax="false"
                           update=":detalleAlmacenGlobalItemForm" target="_blank">
                <h:outputText value="#{item.anno2}">
                    <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                </h:outputText>
                <f:setPropertyActionListener target="#{jsfBean.anno}" value="#{jsfBean.reporteContext.checkPoint.anno2}"/>
                <f:setPropertyActionListener target="#{jsfBean.sociedadId}" value="#{item.sociedadId}"/>
                <f:setPropertyActionListener target="#{jsfBean.checkPointId}" value="#{item.checkPointId}"/>
                <f:setPropertyActionListener target="#{jsfBean.rangoId}" value="#{jsfBean.reporteContext.rangoSelect}"/>
                <f:setPropertyActionListener target="#{jsfBean.rangoOrden}" value="#{item.rango}"/>
            </h:commandLink>

        </p:column>
        <p:column styleClass="columnRightTopClass" style="min-width:30px">
            <h:outputText value="#{item.cantidadAnno3}">
                <f:convertNumber pattern="###,###,##0" locale="en-US"/>
            </h:outputText>
        </p:column>

        <p:column styleClass="columnRightTopClass" style="min-width:30px">
            <f:facet name="header">
                <h:outputText value="#{jsfBean.reporteContext.checkPoint.anno3}"/>
            </f:facet>
            <h:commandLink action="#{jsfBean.onDetalleItemGlobal}" ajax="false"
                           update=":detalleAlmacenGlobalItemForm" target="_blank">
                <h:outputText value="#{item.anno3}">
                    <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                </h:outputText>
                <f:setPropertyActionListener target="#{jsfBean.anno}" value="#{jsfBean.reporteContext.checkPoint.anno3}"/>
                <f:setPropertyActionListener target="#{jsfBean.sociedadId}" value="#{item.sociedadId}"/>
                <f:setPropertyActionListener target="#{jsfBean.checkPointId}" value="#{item.checkPointId}"/>
                <f:setPropertyActionListener target="#{jsfBean.rangoId}" value="#{jsfBean.reporteContext.rangoSelect}"/>
                <f:setPropertyActionListener target="#{jsfBean.rangoOrden}" value="#{item.rango}"/>
            </h:commandLink>

        </p:column>

        <p:columns value="#{jsfBean.columns}" var="column" styleClass="columnRightTopClass" columnIndexVar="colIndex" >
            <f:facet name="header">
                <h:outputText value="#{column.header}" />
            </f:facet>
                <h:commandLink action="#{jsfBean.onDetalleItemGlobal}" ajax="false"
                               update=":detalleAlmacenGlobalItemForm" target="_blank">
                    <h:outputText value="#{item[column.property]}">
                        <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                    </h:outputText>
                    <f:setPropertyActionListener target="#{jsfBean.anno}" value="#{column.header}"/>
                    <f:setPropertyActionListener target="#{jsfBean.sociedadId}" value="#{item.sociedadId}"/>
                    <f:setPropertyActionListener target="#{jsfBean.checkPointId}" value="#{item.checkPointId}"/>
                    <f:setPropertyActionListener target="#{jsfBean.rangoId}" value="#{jsfBean.reporteContext.rangoSelect}"/>
                    <f:setPropertyActionListener target="#{jsfBean.rangoOrden}" value="#{item.rango}"/>
                </h:commandLink>

        </p:columns>
        <p:column styleClass="columnRightTopClass" style="min-width:30px">
            <h:outputText value="#{item.cantidadGeneral}">
                <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
            </h:outputText>
        </p:column>
        <p:column styleClass="columnRightTopClass" style="min-width:30px">
            <h:commandLink action="#{jsfBean.onDetalleItemGlobal}" ajax="false"
                           update=":detalleAlmacenGlobalItemForm" target="_blank">
                <h:outputText value="#{item.totalGeneral}">
                    <f:convertNumber pattern="###,###,##0.00" locale="en-US"/>
                </h:outputText>
                <f:setPropertyActionListener target="#{jsfBean.sociedadId}" value="#{item.sociedadId}"/>
                <f:setPropertyActionListener target="#{jsfBean.checkPointId}" value="#{item.checkPointId}"/>
                <f:setPropertyActionListener target="#{jsfBean.rangoId}" value="#{jsfBean.reporteContext.rangoSelect}"/>
                <f:setPropertyActionListener target="#{jsfBean.rangoOrden}" value="#{item.rango}"/>
            </h:commandLink>
        </p:column>
    </p:dataTable>

</ui:composition>
